/**
 * API方法类
 */
import Axios from 'axios'

class Api {
  async aget ({url, meta}, config = {data: {}}) {
    config.data = this.margeMeta(meta, config.data)
    return Axios.get(url, config)
  }

  async apost ({url, meta}, data, config) {
    return Axios.post(url, this.margeMeta(meta, data), config)
  }

  async aput ({url, meta}, data, config) {
    return Axios.put(url, this.margeMeta(meta, data), config)
  }

  get ({url, meta}, config = {data: {}}) {
    config.data = this.margeMeta(meta, config.data)
    return Axios.get(url, config)
  }

  post ({url, meta}, data, config) {
    return Axios.post(url, this.margeMeta(meta, data), config)
  }

  put ({url, meta}, data, config) {
    return Axios.put(url, this.margeMeta(meta, data), config)
  }

  margeMeta (meta = {}, data = {}) {
    if (data instanceof FormData) {
      for (let key in meta) {
        data.append(key, meta[key].file | meta[key])
      }
      return data
    }
    return Object.keys(data).length > 0 ? Object.assign(data, meta) : meta
  }

  hasClass (elements, cName) {
    return !!elements.className.match(new RegExp('(\\s|^)' + cName + '(\\s|$)'))
  }
  removeClass (elements, cName) {
    if (this.hasClass(elements, cName)) {
      elements.className = elements.className.replace(new RegExp('(\\s|^)' + cName + '(\\s|$)'), ' ')
    }
  }

  getName (name) {
    const index = name.indexOf('.')
    return name.substring(0, index)
  }

  getImage (dataUrl) {
    return new Promise((resolve, reject) => {
      const img = new Image()
      img.onload = function () {
        resolve(this)
      }
      img.src = dataUrl
    })
  }

  readAsDataURL (img) {
    return new Promise((resolve, reject) => {
      const reader = new FileReader()
      reader.addEventListener('load', () => resolve(reader.result))
      reader.readAsDataURL(img)
    })
  }
  getBase64 (img, callback) {
    const reader = new FileReader()
    reader.addEventListener('load', () => callback(reader.result))
    reader.readAsDataURL(img)
  }

  /**
   * 把base64 的 dataUrl 转换为 Blob
   * 比如有一个file.content = 
   * 可以转为Blob  convertBase64UrlToBlob (file.content)
   * 还有可能在图片压缩的时候，一般用canvas进行压缩
   * const canvas = document.createElement('canvas')
   * const dataURL = canvas.toDataURL('image/jpeg', quality) // quality为压缩质量比率
   * 转为Blob  convertBase64UrlToBlob (dataURL)
   * @param {*} dataUrl
   */
  convertBase64UrlToBlob (dataUrl) {
    let arr = dataUrl.split(',')
    let mime = arr[0].match(/:(.*?);/)[1]
    let bstr = atob(arr[1])
    let n = bstr.length
    let u8arr = new Uint8Array(n)
    while (n--) {
      u8arr[n] = bstr.charCodeAt(n)
    }
    return new Blob([u8arr], {type: mime})
  }

  // 将base64转换为blob
  dataURLtoBlob (dataurl) {
    let arr = dataurl.split(',')
    let mime = arr[0].match(/:(.*?);/)[1]
    let bstr = atob(arr[1])
    let n = bstr.length
    let u8arr = new Uint8Array(n)
    while (n--) {
      u8arr[n] = bstr.charCodeAt(n)
    }
    return new Blob([u8arr], { type: mime })
  }
  // 将blob转换为file
  blobToFile (blob, fileName) {
    return new File([blob], fileName, {type: blob.type, lastModified: Date.now()})
  }
}
export default new Api()
